import Vue from 'vue'
import Router from 'vue-router'
import Index from './views/index.vue'
import Login from './views/login.vue'
import Register from './views/register.vue'

Vue.use(Router)

const router =  new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: [
    {
      path: '/',
      name: 'index',
      component: Index,
      children:[
        {
          path:'',
          redirect:'/charts'
        },
        {
          path:'/charts',
          name:'charts',
          component:() => import('./views/Charts.vue')
        },
        {
          path:'/contacts',
          name:'contacts',
          component:()=>import('./views/Contacts.vue')
        },
        {
          path:'/discover',
          name:'discover',
          component:()=>import('./views/Discover.vue')
        },
        {
          path:'/me',
          name:'me',
          component:()=>import('./views/Me.vue')
        },
      ]
    },
    {
      path: '/login',
      name: 'login',
      // 懒加载
      component: () => import("./views/login.vue")
    },
    {
      path: '/register',
      name: 'register',
      component: () => import("./views/register.vue")
    },
    {
      path: '/moments',
      name: 'moments',
      component: () => import("./views/Moments.vue")
    }
    
  ]
})
// 路由守卫
router.beforeEach((to, from, next) => {
  const isLogin = localStorage.wxpyqToken ? true : false;
  if(to.path == '/login' || to.path == '/register'){
    next()
  }else{
    if(isLogin){
      next()
    }else{
      alert('请先登陆')
      next('/login')
    }
  }
})

export default router